/**
 * 初始化数据
 * @param type
 * @param id
 * @param catalogId
 * @returns
 */
function initData(type,id,catalogId){
	if(type=="update"){
		$("#b_url").val("/rest/IotDeviceBusiness/modify");
		$$.ajax({
	        url:"/rest/IotDeviceBusiness/query",
	        suiteCode:"iot",
	        data : {
	            id : id
	        },
	        success : function(data){
	            $("#b_id").val(data.id);
	            $("#b_catalogId").val(data.catalogId);
	            $("#b_deviceCode").val(data.deviceCode);
	            $("#b_deviceName").val(data.deviceName);
	            $("#b_devicePwd").val(data.devicePwd);
	            $("#b_dptlName").val(data.dptlName);
	            $("#b_dptlBusi").val(data.dptlBusi);
	            $("#b_rowSort").val(data.rowSort);
	            var status = data.status;
	            if(status==1){
	            	$('#in-use').attr('checked', 'checked');
	            }else{
	            	$('#not-in-use').attr('checked', 'checked');
	            }
	            $("#b_remarks").val(data.remarks);
	        }
	    });
	}else{
		$("#b_catalogId").val(catalogId);
		$("#b_url").val("/rest/IotDeviceBusiness/add");
	}
}

/**
 * 校验表单
 */
function validForm(){
	return $("#deviceForm").valid();
}

/**
 * 添加表单校验
 */
function addValidate() {
    $("#deviceForm").validate({
        rules: {
            b_deviceCode: {
                required: true,
                rangelength: [1,30]
            },
            b_deviceName: {
                required: true,
                rangelength: [1,30]
            },
            b_devicePwd: {
                required: true,
                rangelength: [1,30]
            },
            b_dptlName: {
            	required: true,
                rangelength: [1,30]
            },
            b_dptlBusi: {
            	required: true,
                rangelength: [1,30]
            },
            b_rowSort:{
                required: true,
                range: [1,9999999]
            },
            b_remarks:{
                maxlength: 250
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errortips",
        success: 'valid',
        messages: {
        	b_deviceCode: {
                required: "(必填)",
                rangelength:"(设备编码必须是{0}到{1}个字符)"
            },
            b_deviceName: {
                required: "(必填)",
                rangelength:"(设备名称必须是{0}到{1}个字符)"
            },
            b_devicePwd: {
                required: "(必填)",
                rangelength:"(设备密码必须是{0}到{1}个字符)"
            },
            b_dptlName: {
                required: "(必填)",
                rangelength:"(协议名称必须是{0}到{1}个字符)"
            },
            b_dptlBusi: {
                required: "(必填)",
                rangelength:"(协议服务类必须是{0}到{1}个字符)"
            },
            b_rowSort: {
                required: "(必填)",
                range:"(排序值必须在{0}和{1}之间)"
            },
            b_remarks: {
                maxlength:"(备注不能超过250个字符)"
            }
        },
    });
}

/**
 * 执行数据保存
 */
function submitForm() {
	var validFormRes = false;
    $$.form({
    	fname:"#deviceForm",
    	suiteCode:"iot",
        async:false,
        able:function(){
            validFormRes = false;
        },
        checkForm:validForm,
        success:function () {
            validFormRes = true;
        }
    });
    return validFormRes;
}

$(function(){
	$('#b_deviceCode').focus();
	addValidate();
});